package com.example.exam.controller;

import com.example.exam.mapper.TQuestionMapper;
import com.example.exam.pojo.vo.QuestionVO.QuestionStandardVO;
import com.example.exam.service.IQuestionService;
import com.example.exam.tools.BaseApiController;
import com.example.exam.tools.JsonPage;
import com.example.exam.tools.JsonResult;
import com.example.exam.tools.JsonUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;


@RestController
@Slf4j
@Api(tags = "题目管理模块")
@RequestMapping("/api/admin/question")
public class QuestionController extends BaseApiController {

    @Autowired
    private TQuestionMapper questionMapper;
    @Autowired
    private IQuestionService questionService;

    @ApiOperation("分页查询题目列表")
    @ApiOperationSupport(order = 200)
    @PostMapping("/page")
    public JsonResult<JsonPage<QuestionStandardVO>> listByPage(@RequestBody QuestionStandardVO questionStandardVO) {
        log.debug("开始处理【分页查询题目列表】的请求");
        JsonPage<QuestionStandardVO> questionByPage = questionService.getQuestionByPage(questionStandardVO);
        return JsonResult.ok(questionByPage);
    }

    @PostMapping("/edit")
    @ApiOperation("添加/修改题目")
    @ApiOperationSupport(order = 220)
    public JsonResult<String> edit(@RequestBody @Validated QuestionStandardVO questionStandardVO) {
        return JsonResult.ok(questionService.edit(questionStandardVO));
    }

    @PostMapping("/delete/{id:[0-9]+}")
    @ApiOperation("删除题目")
    @ApiOperationSupport(order = 230)
    public JsonResult<String> delete(@PathVariable Integer id) {
        log.info("开始处理【删除题目】的请求");
        questionService.delete(id);
        return JsonResult.ok("删除题目成功！");
    }

    @PostMapping("/select/{id:[0-9]+}")
    @ApiOperation("根据id查询题目")
    @ApiOperationSupport(order = 210)
    public JsonResult<QuestionStandardVO> select(@PathVariable Integer id) {
        log.info("开始处理【根据id题目】的请求");
        QuestionStandardVO standardById = questionService.getStandardById(id);
        return JsonResult.ok(standardById);
    }
}
